package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.cargo.FactoryProduct;
import cn.itcast.domain.cargo.FactoryProductExample;
import cn.itcast.service.cargo.FactoryProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.commons.DownloadUtil;
import cn.itcast.utils.commons.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;


@Controller
@RequestMapping(value = "/baseinfo/product")
@SuppressWarnings("all")
public class FactoryProductController extends BaseController {

    @Reference
    private FactoryService factoryService;

    @Reference
    private FactoryProductService factoryProductService;

    @RequestMapping(value = "/list.do",name = "全查/新增页面")
    public  String findAll(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "4") int pageSize){
        //带着生产厂家到页面展示   状态为1
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andStateEqualTo("1");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //查ctype    set集合不能重复
        HashSet set = new HashSet();
        for (Factory factory : factoryList) {
            String ctype = factory.getCtype();
            set.add(ctype);
        }
        request.setAttribute("ctypeList",set);

        //查所有的产品
        PageInfo page = factoryProductService.findAll(pageNum, pageSize);
        request.setAttribute("page",page);
        return "baseinfo/product/factoryproduct-list";
    }

    @RequestMapping(value = "/edit.do",name = "新增/编辑")
    public String edit(FactoryProduct factoryProduct,String describeFactory){
        //先根据factoryName查询
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andFactoryNameEqualTo(describeFactory);
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        //根据factoryName获取factoryId
        String factoryId = null;
        for (Factory factory : factoryList) {
            factoryId = factory.getId();
        }
        //设置factoryId
        factoryProduct.setFactoryId(factoryId);
        //将factoryId带到页面
        request.setAttribute("factoryId",factoryId);

        if (UtilFuns.isEmpty(factoryProduct.getId())){
            //新增时，需要通过factoryName获取factoryId
            //设置主键id
            factoryProduct.setId(UUID.randomUUID().toString().replace("-",""));
            factoryProduct.setFactoryId(factoryId);
            factoryProductService.save(factoryProduct);

        }else {
            //设置体积
            factoryProduct.setBulk(factoryProduct.getSizeHeigth() * factoryProduct.getSizeWidth() * factoryProduct.getSizeLength());
            factoryProductService.update(factoryProduct);
        }
        //展示最新数据
        return "redirect:/baseinfo/product/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "到编辑页面")
    public String toUpdate(String id){
        //带着生产厂家到页面，查询生产厂家状态为1,
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andStateEqualTo("1");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //ctype
        HashSet set = new HashSet();
        for (Factory factory : factoryList) {
            set.add(factory.getCtype());
        }
        request.setAttribute("ctypeList",set);

        //根据id查，回显数据
        FactoryProduct factoryProduct = factoryProductService.findById(id);
        request.setAttribute("factoryProduct",factoryProduct);

        return "baseinfo/product/factoryproduct-update";
    }

    @RequestMapping(value = "/delete.do",name = "删除")
    public String delete(String id){
        factoryProductService.delete(id);
        //展示最新数据
        return "redirect:/baseinfo/product/list.do";
    }
}
